package gamePieces;



import gameComponents.Board;
import gameComponents.Player;

import java.awt.Point;
import java.util.ArrayList;

/**
 * Class for the chess Piece of a King
 *
 */
public class King extends Piece{
	/**
	 * Default constructor for King object.
	 * X and Y coordinates of King are set in the superclass Piece. 
	 * 
	 * @param x coordinate of King
	 * @param y coordinate of King
	 */
	public King(int x, int y, Player player) {
		super(x,y, player);
	}
	
	/**
	 * toString method
	 * @return The string representation of a King on the board.
	 * @author Vivian Andreeva
	 * @modified by Selman Selman
	 */
	public String toString() {
		if (getOwnership().getId()==1) {
		return "K";
		}
		return "k";
	}
	
	/**
	 * Checks if king move is legal
	 * @return true if the move is legal
	 * @author Selman Selman and Jordan Beland
	 */
	public boolean checkIfLegal(int x, int y){
		if(x < 0 || x > Board.SIZE){
			return false;
		} else if(y < 0 || y > Board.SIZE){
			return false;
		} else{
			int xDiff = Math.abs(super.getX() - x);
			int yDiff = Math.abs(super.getY() - y);
			if((yDiff == 1 && xDiff == 0) || (xDiff == 1 && yDiff == 0) || (xDiff == 1 && yDiff == 1)){
				return true;
			}
		}
		return false;
		}

	@Override 
	 /**
	  * Overrides abstract method in superclass Piece.
	  * Calculates list of all possible moves of
	  * the King object at its current position.
	  * King piece can move one square horizontally, vertically or diagonally.
	  * @author Sabrina Gaudreau
	  * @return an Arraylist of all points the king could IDEALLY move to.
	  */
	public ArrayList<Point> possibleMoves(){
			int x = super.getX();
			int y = super.getY();
			moves = new ArrayList<Point>(); //will contain coordinates of all possible moves
			//possible move in the horizontal right
			if((x + 1) < Board.SIZE){
				moves.add(new Point(x+1, y));
			}
			//possible move in the horizontal left
			if((x - 1) >= 0){
				moves.add(new Point(x-1, y));
			}
			//possible move in the vertical down
			if((y + 1) < Board.SIZE){
				moves.add(new Point(x, y+1));
			}
			//possible move in the vertical up
			if((y - 1) >= 0){
				moves.add(new Point(x, y-1));
			}
			//possible move in the down positive diagonal
			if((x + 1) < Board.SIZE && (y + 1) < Board.SIZE){
				moves.add(new Point(x+1, y+1));
			}
			//possible move in the up positive diagonal
			if((x + 1) < Board.SIZE && (y - 1) >= 0){
				moves.add(new Point(x+1, y-1));
			}
			//possible move in the up negative diagonal
			if((x - 1) >= 0 && (y - 1) >= 0){
				moves.add(new Point(x-1, y-1));
			}
			//possible move in the down negative diagonal
			if((x - 1) >= 0 && (y + 1) < Board.SIZE){
				moves.add(new Point(x-1, y+1));
			}
			return moves;
		}
}
